<template>
  <div class="search_container">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" class="top">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>图书查询</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 搜索内容和导出区域 -->
      <el-row style="height: 45px">
        <el-col :span="1.8">
          <span style="line-height: 40px">条件搜索:&emsp;&nbsp;</span>
        </el-col>
        <el-col :span="5">
          <el-select v-model="queryInfo.query" filterable placeholder="请选择" size="large">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-col>
        <el-col :span="2"></el-col>
        <el-col :span="4">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.condition"
            class="input-with-select"
            @keyup.enter.native="searchBookByPage"
            size="large"
          >
            <el-button slot="append" icon="el-icon-search" @click="searchBookByPage"></el-button>
          </el-input>
        </el-col>
        <el-col :span="2">
          <el-button type="primary" class="el-icon-printer" size="large" @click="searchBookByPage">
            <el-icon><Search /></el-icon>
          </el-button>
        </el-col>
        <el-col :span="1">
          <el-button size="large" type="success" @click="AllExport = true">导出所有图书</el-button>
          <el-dialog v-model="AllExport" title="Tips" width="500">
            <span>确定要导出所有图书吗？</span>
            <br />
            <br />
            <span style="color: red">注：导出时间较长，请耐心等待</span>
            <template #footer>
              <div class="dialog-footer">
                <el-button @click="AllExport = false">取消</el-button>
                <el-button type="primary" @click="exportToAll">确认导出</el-button>
              </div>
            </template>
          </el-dialog>
        </el-col>
        <el-col :span="3"></el-col>
        <el-col :span="3">
          <!-- 上面可以自定义自己的样式，还可以引用其他组件button -->
          <el-button type="primary" class="el-icon-printer" size="large" @click="exportToExcel">
            导出当前页
          </el-button>
          <!-- </vue-json-excel> -->
        </el-col>
        <el-col :span="1">
          <el-button type="success" class="el-icon-full-screen" size="large" @click="fullScreen">
            全屏
          </el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table
        :data="tableData"
        height="450"
        :key="time"
        style="width: 100%; font-size: 14px"
        v-loading="loading"
        show-overflow-tooltip
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
        id="pdfDom"
        :default-sort="{ prop: 'bookNumber', order: 'ascending' }"
      >
        <el-table-column prop="bookNumber" label="图书编号" sortable width="130"></el-table-column>
        <el-table-column prop="bookName" label="图书名称"></el-table-column>
        <el-table-column prop="bookAuthor" label="作者"></el-table-column>
        <el-table-column prop="bookLibrary" label="图书馆"></el-table-column>
        <el-table-column prop="bookType" label="分类"></el-table-column>
        <el-table-column prop="bookLocation" label="位置" sortable></el-table-column>
        <el-table-column prop="bookStatus" label="状态" sortable></el-table-column>
        <el-table-column prop="bookDescription" label="描述" width="600px"></el-table-column>
      </el-table>
      <!-- 分页查询区域 -->
      <el-pagination
        @size-change.sync="handleSizeChange"
        @current-change.sync="handleCurrentChange"
        :current-page.sync="queryInfo.pageNum"
        :page-size.sync="queryInfo.pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from "vue";
import { getBookSearch, ExportAllBook } from "../../api/api";
import * as XLSX from "xlsx";

let time = ref(0);
//全部导出框的显示与隐藏
let AllExport = ref(false);
//条件搜索
const options = reactive([
  {
    value: "book_number",
    label: "图书编号",
  },
  {
    value: "book_name",
    label: "图书名称",
  },
  {
    value: "book_author",
    label: "作者",
  },
  {
    value: "book_location",
    label: "位置",
  },
  {
    value: "book_description",
    label: "描述",
  },
]);
//表格数据
let tableData = ref([]);

const queryInfo = reactive({
  //当前页
  pageNum: 1,
  //每页显示个数选择器
  pageSize: 10,
  //具体条件
  condition: "",
  //查询字段
  query: "",
});
//总页数
const total = ref(0);

let title = "图书查询表格";
//表格参数
let json_fields = {
  图书编号: "bookNumber",
  图书名称: "bookName",
  作者: "bookAuthor",
  图书馆: "bookLibrary",
  分类: "bookType",
  位置: "bookLocation",
  状态: "bookStatus",
  描述: "bookDescription",
};
//加载
let loading = ref(true);

onMounted(() => {
  //向后端请求
  searchBookByPage();
});
//导出excel
const exportToExcel = () => {
  //excel表格头部
  const headers = ["图书编号", "图书名称", "作者", "图书馆", "分类", "位置", "状态", "描述"];
  // 转换表格数据为数组
  const ExcleData = tableData.value.map(item => [
    item.bookNumber,
    item.bookName,
    item.bookAuthor,
    item.bookLibrary,
    item.bookType,
    item.bookLocation,
    item.bookStatus,
    item.bookDescription,
  ]);
  // 创建一个工作簿
  const workbook = XLSX.utils.book_new();
  // 创建一个工作表
  const worksheet = XLSX.utils.aoa_to_sheet([headers, ...ExcleData]);

  // 将工作表添加到工作簿
  XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");
  // 生成Excel文件
  XLSX.writeFile(workbook, "图书查询表格.xls");
};

//导出所有图书
const exportToAll = async () => {
  let data = { condition: "", query: "" };
  (data.condition = queryInfo.condition), (data.query = queryInfo.query);
  AllExport.value = false;
  await ExportAllBook(data);
};
//切换每页显示几个
const handleSizeChange = val => {
  queryInfo.pageSize = val;
  searchBookByPage();
};
//切换页码
const handleCurrentChange = val => {
  queryInfo.pageNum = val;
  searchBookByPage();
};
//请求表格数据
const searchBookByPage = () => {
  getBookSearch(queryInfo)
    .then(res => {
      total.value = res.data.total;
      tableData.value = res.data.records;
      // time.value = Math.random();
      console.log(tableData.value);
      ElMessage({
        message: "获取信息成功",
        type: "success",
      });
    })
    .catch(err => {
      console.log(err, "err");
      if (err.response.data.message == "Book Not Found") {
        ElMessage.error("查询为空");
      } else {
        ElMessage.error("获取信息失败");
      }
    });
  loading.value = false;
};
//下载pdf
const downLoad = () => {
  getPdf(title); //参数是下载的pdf文件名
};
//全屏
const fullScreen = () => {
  // Dom对象的一个属性: 可以用来判断当前是否为全屏模式(trueORfalse)
  let full = document.fullscreenElement;
  // 切换为全屏模式
  if (!full) {
    // 文档根节点的方法requestFullscreen实现全屏模式
    document.documentElement.requestFullscreen();
  } else {
    // 退出全屏模式
    document.exitFullscreen();
  }
};
</script>

<style scoped>
.search_container {
  .top {
    margin-bottom: 8px;
  }
}
</style>
